/**
 * Copyright (c)  HeiFeiDeFengZheng.Co.Ltd. All rights reserved.
 **/
package com.defu.newmall.service.impl;

import com.defu.newmall.dao.AdminUserDao;
import com.defu.newmall.dao.NewBeeAdminUserTokenDao;
import com.defu.newmall.entity.AdminUser;
import com.defu.newmall.entity.AdminUserToken;
import com.defu.newmall.service.AdminUserService;
import com.defu.newmall.utils.NumberUtil;
import com.defu.newmall.utils.SystemUtil;
import com.defu.newmall.vo.UpdateAdminNameParam;
import com.defu.newmall.vo.UpdateAdminPasswordParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * 功能描述：
 *
 * @author huifeidefengzheng
 * @since 2024-01-28
 *
 **/
@Slf4j
@Service
public class AdminUserServiceImpl implements AdminUserService {

    @Autowired
    private AdminUserDao adminUserDao;

    @Autowired
    private NewBeeAdminUserTokenDao newBeeAdminUserTokenDao;

    @Override
    public AdminUserToken login(String userName, String passwordMd5) {
        log.info("passwordMd5=" + passwordMd5);
        AdminUser loginAdminUser = adminUserDao.login(userName, passwordMd5);
        AdminUserToken adminUserToken = null;
        if (loginAdminUser != null) {
            //登录后即执行修改token的操作
            String token = getNewToken(System.currentTimeMillis() + "", loginAdminUser.getAdminUserId());
            adminUserToken = newBeeAdminUserTokenDao.selectByPrimaryKey(loginAdminUser.getAdminUserId());
            //当前时间
            Date now = new Date();
            //过期时间
            Date expireTime = new Date(now.getTime() + 2 * 24 * 3600 * 1000);//过期时间 48 小时
            if (adminUserToken == null) {
                adminUserToken = new AdminUserToken();
                adminUserToken.setAdminUserId(loginAdminUser.getAdminUserId());
                adminUserToken.setToken(token);
                adminUserToken.setUpdateTime(now);
                adminUserToken.setExpireTime(expireTime);
                //新增一条token数据
                if (newBeeAdminUserTokenDao.insertSelective(adminUserToken) > 0) {
                    //新增成功后返回
                    return adminUserToken;
                }
            } else {
                adminUserToken.setToken(token);
                adminUserToken.setUpdateTime(now);
                adminUserToken.setExpireTime(expireTime);
                //更新
                if (newBeeAdminUserTokenDao.updateByPrimaryKeySelective(adminUserToken) > 0) {
                    //修改成功后返回
                    return adminUserToken;
                }
            }

        }
        return adminUserToken;
    }

    @Override
    public AdminUser getUserDetailById(Long adminUserId) {
        return adminUserDao.selectByPrimaryKey(adminUserId);
    }

    @Override
    public boolean updateName(UpdateAdminNameParam adminNameParam) {
        Long adminid = Long.valueOf(adminNameParam.getAdminUserId());
        AdminUser adminUser = adminUserDao.selectByPrimaryKey(adminid);
        //当前用户非空才可以进行更改
        if (adminUser != null) {
            //设置新名称并修改
            adminUser.setLoginUserName(adminNameParam.getLoginUserName());
            adminUser.setNickName(adminNameParam.getNickName());
            if (adminUserDao.updateByPrimaryKeySelective(adminUser) > 0) {
                //修改成功则返回true
                return true;
            }
        }
        return false;
    }

    @Override
    public boolean updatePassword(UpdateAdminPasswordParam adminPasswordParam) {
        AdminUser adminUser = adminUserDao.selectByPrimaryKey(adminPasswordParam.getAdminUserId());
        //当前用户非空才可以进行更改
        if (adminUser != null) {
            //比较原密码是否正确
            if (adminPasswordParam.getOriginalPassword().equals(adminUser.getLoginPassword())) {
                //设置新密码并修改
                adminUser.setLoginPassword(adminPasswordParam.getNewPassword());
                if (adminUserDao.updateByPrimaryKeySelective(adminUser) > 0) {
                    //修改成功且清空当前token则返回true
                    return true;
                }
            }
        }
        return false;
    }

    @Override
    public void logout(Long adminUserId) {
        adminUserDao.deleteByPrimaryKey(adminUserId);
    }

    /**
     * 获取token值
     *
     * @param timeStr
     * @param userId
     * @return
     */
    private String getNewToken(String timeStr, Long userId) {
        String src = timeStr + userId + NumberUtil.genRandomNum(6);
        return SystemUtil.genToken(src);
    }
}
